<?php
/**
 * Created by PhpStorm.
 * User: 黑盒子
 * Date: 2020/11/18
 * Time: 0:00
 */

namespace app\api\controller;

use app\common\exception\ApiException;
use app\api\model\ResourceExtend;
use think\Db;

class Resource extends Base
{
    public function listing()
    {
        $__page_size = input('__page_size',10);
        $list = Db::name('t_resource')->alias('t')
            ->join('t_type type','t.type_id = type.id')
            ->where(['t.is_delete' => 0])
            ->field('t.id,t.type_id,t.version,t.issue_time,t.intro,type.name as typename')
            ->paginate($__page_size)->each(function ($item,$key){
                $resource = ResourceExtend::where('resource_id' ,$item['id'])->field('cloud_disk,url,password')->select();
                $item['resource_list'] = $resource;
                return $item;
            });

        return json(['success' => 1, 'list' => $list]);
    }

    public function delete()
    {
        $id = input('id');

        if ($id) {
            $exist = \app\api\model\Resource::where('is_delete', 0)->where('id', $id)->find();

            if (!$exist) {
                throw new ApiException(['msg' => '该类型不存在或已被删除']);
            }

            $rst = \app\api\model\Resource::update(['is_delete' => 1], ['id' => $id]);
            if (!$rst) {
                throw new ApiException(['msg' => '操作失败']);
            }
        } else {
            throw new ApiException(['msg' => '参数错误']);
        }

        return json(['success' => 1, 'msg' => '操作成功']);
    }

    public function alt()
    {
        $id = input('id');

        $data = input('param.','[]');

        if (empty($data['type_id'])) {
            throw new ApiException(['msg' => '请选择适用无盘']);
        } else if (empty($data['version'])) {
            throw new ApiException(['msg' => '请填写系统版本']);
        } elseif (empty($data['issue_time'])) {
            throw new ApiException(['msg' => '请填写发布日期']);
        } elseif (empty($data['intro'])) {
            throw new ApiException(['msg' => '请填写说明']);
        } elseif (empty($data['resource_list'])) {
            throw new ApiException(['msg' => '请填网盘信息']);
        }

        $resource_data = [
            'type_id' => $data['type_id'],
            'version' => $data['version'],
            'issue_time' => $data['issue_time'],
            'intro' => $data['intro'],
        ];

        Db::startTrans();
        try {
            if ($id) {
                $exist = \app\api\model\Resource::where('is_delete',0)->where('id',$id)->find();
                if (!$exist) {
                    throw new ApiException(['msg' => '数据不存在或已被删除']);
                }
                $rst = \app\api\model\Resource::update($resource_data, ['id' => $id]);
                if (!$rst) {
                    throw new ApiException(['msg' => '操作失败']);
                }
            } else {
                $resource = \app\api\model\Resource::create($resource_data);
                if (!$resource->id) {
                    throw new ApiException(['msg' => '操作失败']);
                }
                $id = $resource->id;
            }

            \app\api\model\ResourceExtend::where('resource_id',$id)->delete();

            $resource_extend_data = json_decode($data['resource_list'],true);
            foreach ($resource_extend_data as $k => $extend_datum) {
                $resource_extend_data[$k]['resource_id'] = $id;
            }

            $resource_extend = new \app\api\model\ResourceExtend();
            $resource_extend->saveAll($resource_extend_data);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new ApiException(['msg' => '操作失败']);
        }

        return json(['success' => 1, 'msg' => '操作成功']);
    }
}